<?php
namespace app\techn_api_n\controller\v1;
use think\Db;
use app\service\TechnService;
use app\service\GaodeService;
class Techn extends Base
{
	public function _initialize()
	{
		parent::_initialize();
	}

	/**
	 * 技师提交初审
	 */
	public function apply_step1()
	{
		$this->IsLogin(); //验证登录
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['user'] = $this->user;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'real_name' => 'require',
				'sex' => 'require',
				'age' => 'require|integer|egt:0',
				'work_year' => 'require|integer|egt:0',
				'mobile' => ['require', 'regex' => '^1((3|4|5|6|7|8|9){1}\d{1})\d{8}$'],
				'county' => 'require'
			],
			[
				'real_name.require' => '请输入真实姓名',
				'sex.require' => '请选择性别',
				'age.require' => '请输入年龄',
				'age.integer' => '年龄只能为整数',
				'age.egt' => '年龄只能大于等于0',
				'work_year.require' => '请输入从业年份',
				'work_year.integer' => '从业年份只能为整数',
				'work_year.egt' => '从业年份只能大于等于0',
				'mobile.require' => '请输入手机号',
				'mobile.regex' => '手机格式有误',
				'county.require' => '请选择意向工作城市'
			]
		);
		if(true !== $validate)
		{
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		//判断是否上传个人生活照
		if(count($params['self_img_arr']) <= 0)
        {
        	echo json_encode(['code' => -1, 'msg' => '请上传个人生活照']);
			exit();
        }
        //判断是否已成为技师
        $techn_info = Db::name('techn')->where('user_id=:user_id')->bind(['user_id' => $params['user']['id']])->find();
        if(!empty($techn_info))
        {
        	echo json_encode(['code' => -1, 'msg' => '已是技师无需申请']);
			exit();
        }
        //判断是否有申请信息
        //判断是添加还是修改查询条件
        $techn_apply_map = '';
        $techn_apply_bind = [];
        if(empty($params['id']))
        {
        	//添加
        	$techn_apply_map = 'user_id=:user_id AND type=:type AND status IN (:status) AND is_delete_time=:is_delete_time';
        	$techn_apply_bind = ['user_id' => $params['user']['id'], 'type' => 1, 'status' => '0,1,2', 'is_delete_time' => 0];
        }else
        {
        	//修改
        	$techn_apply_map = 'user_id=:user_id AND type=:type AND status IN (:status) AND is_delete_time=:is_delete_time AND id<>:id';
        	$techn_apply_bind = ['user_id' => $params['user']['id'], 'type' => 1, 'status' => '0,1,2', 'is_delete_time' => 0, 'id' => $params['id']];
        }
        $techn_apply_info = Db::name('techn_apply')->where($techn_apply_map)->bind($techn_apply_bind)->find();
        if(!empty($techn_apply_info))
        {
        	echo json_encode(['code' => -1, 'msg' => '用户已存在申请信息']);
			exit();
        }
        //查询选择地区是否开启
        //判断是添加还是更新
        //基础数据
        $data = [
        	'real_name' => $params['real_name'],
        	'sex' => $params['sex'],
        	'age' => $params['age'],
        	'work_year' => $params['work_year'],
        	'mobile' => $params['mobile'],
        	'province' => $params['province'],
        	'city' => $params['city'],
        	'county' => $params['county'],
        	'province_name' => Db::name('area')->where(['id' => $params['province']])->value('name'),
        	'city_name' => Db::name('area')->where(['id' => $params['city']])->value('name'),
        	'county_name' => Db::name('area')->where(['id' => $params['county']])->value('name'),
        	'self_img' => implode(",", $params['self_img_arr']),
        	'type' => 1,
        	'steps' => TechnService::$type_apply_array[1]['steps'],
        	'no_step' => 1
        ];
        if(empty($params['id']))
        {
        	//添加
	        //写入主表
	        $data['user_id'] = $params['user']['id'];
	        $data['add_time'] = time();
        	$data['upd_time'] = time();
	        //启动事务
	        Db::startTrans();
	        $techn_apply_id = Db::name('techn_apply')->insertGetId($data);
	        if($techn_apply_id <= 0)
	        {
	        	//回滚事务
	        	Db::rollback();
	        	echo json_encode(['code' => -1, 'msg' => '申请失败']);
				exit();
	        }
	        //写入附表
	        $techn_audit_id = Db::name('techn_audit')->insertGetId([
	        	'techn_apply_id' => $techn_apply_id,
	        	'no_step' => 1,
	        	'apply_time' => time(),
	        	'add_time' => time(),
	        	'upd_time' => time()
	        ]);
	        if($techn_audit_id <= 0)
	        {
	        	//回滚事务
	        	Db::rollback();
	        	echo json_encode(['code' => -1, 'msg' => '申请失败']);
				exit();
	        }
	        //提交事务
	        Db::commit();
	        echo json_encode(['code' => 0, 'msg' => '申请成功，等待管理员审核']);
			exit();
	    }else
	    {
	    	//修改
	    	$data['upd_time'] = time();
	    	//查询主表信息
	    	$up_info = Db::name('techn_apply')->where(['id' => $params['id'], 'user_id' => $params['user']['id'], 'is_delete_time' => 0])->find();
	    	if(empty($up_info))
	    	{
	    		echo json_encode(['code' => -1, 'msg' => '审核信息不存在']);
				exit();
	    	}
	    	if($up_info['no_step'] != 1)
	    	{
	    		echo json_encode(['code' => -1, 'msg' => '禁止修改']);
				exit();
	    	}
	    	if($up_info['status'] != 0)
	    	{
	    		echo json_encode(['code' => -1, 'msg' => '禁止修改']);
				exit();
	    	}
	    	if(Db::name('techn_apply')->where(['id'=>$up_info['id']])->update($data))
	    	{
	    		echo json_encode(['code' => 0, 'msg' => '申请成功，等待管理员审核']);
				exit();
	    	}else
	    	{
	    		echo json_encode(['code' => -1, 'msg' => '申请失败']);
				exit();
	    	}
	    }
        echo json_encode($result);
	}

	/**
	 * 获取技师审核信息
	 */
	public function audit_info()
	{
		$this->IsLogin(); //验证登录
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['user'] = $this->user;
		//dump($params);
		//查询技师申请信息
		$info = Db::name('techn_apply')->where(['user_id' => $params['user']['id'], 'type' => 1, 'is_delete_time' => 0])->order('id desc')->find();
		if(!empty($info))
		{
			$info['self_img_arr'] = !empty($info['self_img']) ? explode(",", $info['self_img']) : [];
			//获取审核状态
			$info['techn_audit'] = Db::name('techn_audit')->where(['techn_apply_id' => $info['id']])->column('no_step,status');
			//处理服务项目
			$info['service_ids_arr'] = !empty($info['service_ids']) ? explode(",", trim($info['service_ids'], ",")) : [];
			//处理身份证照片
			$info['id_card_img_arr'] = !empty($info['id_card_img']) ? explode(",", $info['id_card_img']) : [];
			//处理资格证书
			$info['diploma_img_arr'] = !empty($info['diploma_img']) ? explode(",", $info['diploma_img']) : [];

			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $info;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '暂无数据';
		}
		echo json_encode($result);
	}

	/**
	 * 技师完善资料(提交复审)
	 */
	public function apply_step2()
	{
		$this->IsLogin(); //验证登录
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['user'] = $this->user;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'id' => 'require',
				'nickname' => 'require',
				'address' => 'require',
				'lng' => 'require',
				'lat' => 'require',
				'descs' => 'require',
				'work_img' => 'require'
			],
			[
				'id.require' => '初审信息不存在，请重新提交',
				'nickname.require' => '请输入昵称',
				'address.require' => '请选择所在地址',
				'lng.require' => '请选择所在地址',
				'lat.require' => '请选择所在地址',
				'descs.require' => '请输入技师简介',
				'work_img.require' => '请上传工作形象照'
			]
		);
		if(true !== $validate)
		{
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		//判断是否选择服务
		if(count($params['service_ids_arr']) <= 0)
		{
			echo json_encode(['code' => -1, 'msg' => '请选择服务项目']);
			exit();
		}
		//判断上传身份证正面
		if(empty($params['id_card_img_arr'][0]))
		{
			echo json_encode(['code' => -1, 'msg' => '请上传身份证正面']);
			exit();
		}
		//判断上传身份证反面
		if(empty($params['id_card_img_arr'][1]))
		{
			echo json_encode(['code' => -1, 'msg' => '请上传身份证反面']);
			exit();
		}
		//判断上传手持身份证
		if(empty($params['id_card_img_arr'][2]))
		{
			echo json_encode(['code' => -1, 'msg' => '请上传手持身份证']);
			exit();
		}
		//判断上传资格证书
		if(count($params['diploma_img_arr']) <= 0)
		{
			echo json_encode(['code' => -1, 'msg' => '请上传资格证书']);
			exit();
		}
		$techn_apply_info = Db::name('techn_apply')->where(['id' => $params['id'], 'is_delete_time' => 0])->find();
		if(empty($techn_apply_info))
		{
			echo json_encode(['code' => -1, 'msg' => '初审信息不存在']);
			exit();
		}
		if($techn_apply_info['audit_step'] != 1)
		{
			echo json_encode(['code' => -1, 'msg' => '审核信息不可修改']);
			exit();
		}
		//修改并插入复审信息
		$no_step = 2;
		//查询复审状态表
		$techn_audit_info = Db::name('techn_audit')->where(['techn_apply_id' => $techn_apply_info['id'], 'no_step' => $no_step])->find();
		//启动事务
		Db::startTrans();
		try{
			//更新申请主表
			Db::name('techn_apply')->where(['id' => $techn_apply_info['id']])->update([
				'nickname' => $params['nickname'],
				'address' => $params['address'],
				'lng' => $params['lng'],
				'lat' => $params['lat'],
				'service_ids' => ",".implode(",", $params['service_ids_arr']).",",
				'descs' => $params['descs'],
				'id_card_img' => implode(",", $params['id_card_img_arr']),
				'diploma_img' => implode(",", $params['diploma_img_arr']),
				'work_img' => $params['work_img'],
				'video' => $params['video'],
				'no_step' => $no_step,
				'upd_time' => time()
			]);
			//判断是是否要插入审核状态表
			if(empty($techn_audit_info))
			{
				//写入附表
				$techn_audit_id = Db::name('techn_audit')->insertGetId([
					'techn_apply_id' => $techn_apply_info['id'],
					'no_step' => $no_step,
					'apply_time' => time(),
					'add_time' => time(),
					'upd_time' => time()
				]);
			}
			//提交事务
			Db::commit();
			$result['code'] = 0;
			$result['msg'] = "申请成功，等待管理员审核";
		} catch (\Exception $e) {
			//回滚事务
			Db::rollback();
			$result['code'] = -1;
			$result['msg'] = "申请失败";
		}
		echo json_encode($result);
	}

	/**
	 * 获取技师服务时间
	 */
	public function pre_time()
	{
		//$this->IsLogin(); //验证登录
		//$this->IsTechn(); //验证技师
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$techn = $this->techn;
		//dump($params);
		
		$list_data = [];
		$week_arr = ["周日","周一","周二","周三","周四","周五","周六"];
		//开始时间
		$start_time = strtotime(date('Y-m-d', time()));
		//结束时间
		$end_time = strtotime(date('Y-m-d', strtotime("+2 day")));
		//算出开始时间与结束时间中间间隔的时间
		$key = 0;
		for($i = $start_time; $i <= $end_time; $i += 86400)
		{
			$list_data[$key]['day'] = date('m-d', $i);
			$list_data[$key]['week'] = $week_arr[date("w", $i)];

			$time_slot = [];
			$slot_key = 0;
			//开始时间段
			$start_slot = strtotime(date('Y-m-d', $i).' 00:00:00');
			//结束时间段
			$end_slot = strtotime(date('Y-m-d', $i).' 23:59:59');
			//判断是否选择接单开始时间与结束时间
			$work_start_hour = !empty($techn['work_start_hour']) ? strtotime(date('Y-m-d', $i).' '.$techn['work_start_hour'].':00') : strtotime(date('Y-m-d', $i).' 00:00:00');
			$work_end_hour = !empty($techn['work_end_hour']) ? strtotime(date('Y-m-d', $i).' '.$techn['work_end_hour'].':00') : strtotime(date('Y-m-d', $i).' 23:59:59');
			if(!empty($params['work_start_hour']))
			{
				$work_start_hour = strtotime(date('Y-m-d', $i).' '.$params['work_start_hour'].':00');
			}
			if(!empty($params['work_end_hour']))
			{
				$work_end_hour = strtotime(date('Y-m-d', $i).' '.$params['work_end_hour'].':00');
			}
			for($j = $start_slot; $j <= $end_slot; $j += 1800)
			{
				//如果大于当前时间才显示
				if($work_start_hour > $work_end_hour)
				{
					if($j >= time() && ($j >= $work_start_hour || $j <= $work_end_hour))
					{
						$time_slot[$slot_key]['time'] = date('H:i', $j);
						$time_slot[$slot_key]['date'] = date('Y-m-d H:i:s', $j);
						$slot_key++;
					}
				}elseif ($work_start_hour < $work_end_hour) {
					if($j >= time() && $j >= $work_start_hour && $j <= $work_end_hour)
					{
						$time_slot[$slot_key]['time'] = date('H:i', $j);
						$time_slot[$slot_key]['date'] = date('Y-m-d H:i:s', $j);
						$slot_key++;
					}
				}else
				{
					if($j >= time())
					{
						$time_slot[$slot_key]['time'] = date('H:i', $j);
						$time_slot[$slot_key]['date'] = date('Y-m-d H:i:s', $j);
						$slot_key++;
					}
				}
			}
			$list_data[$key]['time_slot'] = $time_slot;
			$key++;
		}
		//dump($list_data);
		//die();
		$result['code'] = 0;
	    $result['msg'] = "请求成功";
	    $result['data'] = $list_data;
	    echo json_encode($result);
	}

	/**
	 * 技师保存工作状态、时间
	 */
	public function set_work_time()
	{
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'work_status' => 'require'
			],
			[
				'work_status.require' => '请选择接单状态'
			]
		);
		if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        $techn_info = Db::name('techn')->where(['id' => $params['techn']['id']])->find();
        if($techn_info['work_status'] == 3)
        {
        	echo json_encode(['code' => -1, 'msg' => '您已下线禁止操作']);
            exit();
        }
        //工作状态为接单 开始计时, 休息, 结束计时, 加入在线时长
        if ($params['work_status'] == 1){
            $update = ['star_time'=>time()];
            Db::name('techn_last')->where('techn_id',$params['techn']['id'])->update($update);
        }
        if ($params['work_status'] == 2){
            $star_time = Db::name('techn_last')->where('techn_id',$params['techn']['id'])->find();
            if ($star_time['star_time'] != 0){
                $line_time_s = time()-$star_time['star_time'];
                $line_time_h = (int)($line_time_s/60/60);
                $line_time_m = (int)(($line_time_s-$line_time_h*3600)/60);
                if ($line_time_m>30) $line_time_h += 0.5;
                $line_time = $line_time_h;
            }else{
                $line_time = 4;
            }
            $have_time = Db::name('techn_last')->where('techn_id',$params['techn']['id'])->field('total_duration')->find();
            $update = $have_time['total_duration']+$line_time;
            Db::name('techn_last')->where('techn_id',$params['techn']['id'])->update(['total_duration'=>$update,'star_time'=>0]);
        }
        //更新数据
        $data = [
        	'work_status' => $params['work_status'],
        	'work_start_hour' => $params['work_start_hour'],
        	'work_end_hour' => $params['work_end_hour'],
        	'no_pre_hour_slot' => !empty($params['no_pre_hour_slot']) ? json_encode($params['no_pre_hour_slot']) : '',
        	'upd_time' => time()
        ];
        if(Db::name('techn')->where(['id' => $params['techn']['id']])->update($data))
        {
            
		    TechnService::TechnPreTimey($params['techn']['id']);
		
        	$result['code'] = 0;
	        $result['msg'] = "设置成功";
        }else
        {
        	$result['code'] = -1;
	        $result['msg'] = "设置失败";
        }
        echo json_encode($result);
	}

	/**
	 * 获取技师服务时间
	 */
	public function get_work_time()
	{
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
		//$params['techn'] = $this->techn;
		$techn = $this->techn;
		$result['code'] = 0;
	    $result['msg'] = "请求成功";
	    $result['data'] = [
    		'work_status' => !empty($techn) ? $techn['work_status'] : '',
    		'work_start_hour' => !empty($techn) ? $techn['work_start_hour'] : '',
    		'work_end_hour' => !empty($techn) ? $techn['work_end_hour'] : '',
    		'no_pre_hour_slot' => !empty($techn['no_pre_hour_slot']) ? json_decode($techn['no_pre_hour_slot'], true) : [],
    	];
	    echo json_encode($result);
	}

	/**
	 * 技师更新位置
	 */
	public function seat_update()
	{
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'lng' => 'require',
				'lat' => 'require',
//				'address' => 'require'
			],
			[
				'lng.require' => '坐标有误',
				'lat.require' => '坐标有误',
//				'address.require' => '坐标有误'
			]
		);
		if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        //根据坐标获取当前省、市、区/县
        //获取高德解地址
        $ni_dili_data = http_curl('https://restapi.amap.com/v3/geocode/regeo?output=json&location='.$params['lng'].','.$params['lat'].'&key='.GaodeService::$web_api_key);
        $ni_dili_data = json_decode($ni_dili_data, true);
        //dump($ni_dili_data);
        if($ni_dili_data['status'] != 1)
        {
        	echo json_encode(['code' => -1, 'msg' => '设置失败']);
            exit();
        }else
        {
        	//根据行政区编码返查出省、市、区/县
        	$adcode = $ni_dili_data['regeocode']['addressComponent']['adcode'];
        	if(empty($adcode))
        	{
        		echo json_encode(['code' => -1, 'msg' => '设置失败']);
                exit();
        	}else
        	{
        		//查询当前区/县
        		$county_info = Db::name('area')->where(['id' => $adcode, 'lev' => 3])->find();
        		//查询当前市
        		$city_info = Db::name('area')->where(['id' => $county_info['pid'], 'lev' => 2])->find();
        		//查询当前省
        		$province_info = Db::name('area')->where(['id' => $city_info['pid'], 'lev' => 1])->find();
        	}
        	//获取格式化地址
        	$address = $ni_dili_data['regeocode']['formatted_address'];
        }
        $data = [
        	'province' => isset($province_info['id']) ? $province_info['id'] : 0,
        	'city' => isset($city_info['id']) ? $city_info['id'] : 0,
        	'county' => isset($county_info['id']) ? $county_info['id'] : 0,
        	'province_name' => isset($province_info['name']) ? $province_info['name'] : '',
        	'city_name' => isset($city_info['name']) ? $city_info['name'] : '',
        	'county_name' => isset($county_info['name']) ? $county_info['name'] : '',
//        	'address' => $params['address'],
        	'address' => $address,
        	'lng' => $params['lng'],
        	'lat' => $params['lat'],
        	'upd_time' => time()
	    ];
	    //dump($data);
	    if(Db::name('techn_last')->where(['techn_id' => $params['techn']['id']])->update($data) !== false)
	    {
	    	$result['code'] = 0;
	        $result['msg'] = "设置成功";
	    }else
	    {
	    	$result['code'] = -1;
	        $result['msg'] = "设置失败";
	    }
	    echo json_encode($result);
	}

	/**
	 * 获取技师修改审核信息
	 */
	public function edit_audit()
	{
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['user'] = $this->user;
		$params['techn'] = $this->techn;
		//dump($params);
		//技师信息
		$info = [];
		//查询技师修改申请信息
		$apply_info = Db::name('techn_apply')->where(['user_id' => $params['user']['id'], 'type' => 2, 'status' => ['LT', 2], 'is_delete_time' => 0])->order('id desc')->find();
		if(!empty($apply_info))
		{
			$info = $apply_info;
		}else
		{
			//技师最终信息
			$info = $params['techn'];
			$info['id'] = '';
		}
		if(!empty($info))
		{
			$info['self_img_arr'] = !empty($info['self_img']) ? explode(",", $info['self_img']) : [];
			//处理服务项目
			$info['service_ids_arr'] = !empty($info['service_ids']) ? explode(",", trim($info['service_ids'], ",")) : [];
			//处理身份证照片
			$info['id_card_img_arr'] = !empty($info['id_card_img']) ? explode(",", $info['id_card_img']) : [];
			//处理资格证书
			$info['diploma_img_arr'] = !empty($info['diploma_img']) ? explode(",", $info['diploma_img']) : [];
			
            $info['descs']=strip_tags($info['descs']);
        
			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $info;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '暂无数据';
		}
		echo json_encode($result);
	}

	/**
	 * 技师提交修改资料审核
	 */
	public function edit_apply()
	{
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['user'] = $this->user;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'real_name' => 'require',
				'sex' => 'require',
				'age' => 'require|integer|egt:0',
				'work_year' => 'require|integer|egt:0',
				'mobile' => ['require', 'regex' => '^1((3|4|5|6|7|8|9){1}\d{1})\d{8}$'],
				'county' => 'require',

				'nickname' => 'require',
				'address' => 'require',
				'lng' => 'require',
				'lat' => 'require',
				'descs' => 'require',
				'work_img' => 'require'
			],
			[
				'real_name.require' => '请输入真实姓名',
				'sex.require' => '请选择性别',
				'age.require' => '请输入年龄',
				'age.integer' => '年龄只能为整数',
				'age.egt' => '年龄只能大于等于0',
				'work_year.require' => '请输入从业年份',
				'work_year.integer' => '从业年份只能为整数',
				'work_year.egt' => '从业年份只能大于等于0',
				'mobile.require' => '请输入手机号',
				'mobile.regex' => '手机格式有误',
				'county.require' => '请选择意向工作城市',

				'nickname.require' => '请输入昵称',
				'address.require' => '请选择所在地址',
				'lng.require' => '请选择所在地址',
				'lat.require' => '请选择所在地址',
				'descs.require' => '请输入技师简介',
				'work_img.require' => '请上传工作形象照'
			]
		);
		if(true !== $validate)
		{
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		//判断是否上传个人生活照
		if(count($params['self_img_arr']) <= 0)
        {
        	echo json_encode(['code' => -1, 'msg' => '请上传个人生活照']);
			exit();
        }
        //判断是否选择服务
        if(count($params['service_ids_arr']) <= 0)
		{
			echo json_encode(['code' => -1, 'msg' => '请选择服务项目']);
			exit();
		}
		//判断上传身份证正面
		if(empty($params['id_card_img_arr'][0]))
		{
			echo json_encode(['code' => -1, 'msg' => '请上传身份证正面']);
			exit();
		}
		//判断上传身份证反面
		if(empty($params['id_card_img_arr'][1]))
		{
			echo json_encode(['code' => -1, 'msg' => '请上传身份证反面']);
			exit();
		}
		//判断上传手持身份证
		if(empty($params['id_card_img_arr'][2]))
		{
			echo json_encode(['code' => -1, 'msg' => '请上传手持身份证']);
			exit();
		}
		//判断上传资格证书
		if(count($params['diploma_img_arr']) <= 0)
		{
			echo json_encode(['code' => -1, 'msg' => '请上传资格证书']);
			exit();
		}
		$techn_apply_info = Db::name('techn_apply')->where(['id' => $params['id'], 'is_delete_time' => 0])->find();
		if(!empty($techn_apply_info))
		{
			echo json_encode(['code' => -1, 'msg' => '申请信息已存在']);
			exit();
		}
		$money=Db::name('techn')->where('user_id',$techn_apply_info['user_id'])->value('money');
		if(!empty($money))
		{
			echo json_encode(['code' => -1, 'msg' => '账户存在余额,不可提交审核']);
			exit();
		}
		//基础数据
		$data = [
        	'real_name' => $params['real_name'],
        	'sex' => $params['sex'],
        	'age' => $params['age'],
        	'work_year' => $params['work_year'],
        	'mobile' => $params['mobile'],
        	'province' => $params['province'],
        	'city' => $params['city'],
        	'county' => $params['county'],
        	'province_name' => Db::name('area')->where(['id' => $params['province']])->value('name'),
        	'city_name' => Db::name('area')->where(['id' => $params['city']])->value('name'),
        	'county_name' => Db::name('area')->where(['id' => $params['county']])->value('name'),
        	'self_img' => implode(",", $params['self_img_arr']),
        	'nickname' => $params['nickname'],
			'address' => $params['address'],
			'lng' => $params['lng'],
			'lat' => $params['lat'],
			'service_ids' => ",".implode(",", $params['service_ids_arr']).",",
			'descs' => $params['descs'],
			'id_card_img' => implode(",", $params['id_card_img_arr']),
			'diploma_img' => implode(",", $params['diploma_img_arr']),
			'work_img' => $params['work_img'],
			'video' => $params['video'],
			'sos_phone' => $params['sos_phone'],
        	'type' => 2,
        	'steps' => TechnService::$type_apply_array[2]['steps'],
        	'no_step' => 1
        ];
        //添加
        //写入主表
        $data['user_id'] = $params['user']['id'];
        $data['add_time'] = time();
        $data['upd_time'] = time();
        //启动事务
        Db::startTrans();
        $techn_apply_id = Db::name('techn_apply')->insertGetId($data);
        if($techn_apply_id <= 0)
        {
        	//回滚事务
        	Db::rollback();
        	echo json_encode(['code' => -1, 'msg' => '申请失败']);
			exit();
        }
        //写入附表
        $techn_audit_id = Db::name('techn_audit')->insertGetId([
        	'techn_apply_id' => $techn_apply_id,
        	'no_step' => 1,
        	'apply_time' => time(),
        	'add_time' => time(),
        	'upd_time' => time()
        ]);
        if($techn_audit_id <= 0)
        {
        	//回滚事务
        	Db::rollback();
        	echo json_encode(['code' => -1, 'msg' => '申请失败']);
			exit();
        }
        //提交事务
        Db::commit();
        echo json_encode(['code' => 0, 'msg' => '申请成功，等待管理员审核']);
        exit();
	}
}